import axios from 'axios';
import qs from 'qs'
import { message } from "antd";
import { getParams } from '&helpers/url';
// import _ from 'lodash';
const API = {
  //管理员
    QUERY_MEMBER_LIST: '/admin/selectAll',   //查询所有用户
    QUERY_MEMBER_ADD:'/admin/add',          //添加用户
    QUERY_MEMBER_DELETE:'/admin/deleteByUsername',    //删除用户
    QUERY_MEMBER_EDIT:'/admin/updateUser', //修改信息
    // QUERY_MEMBER_RESET
  //单位
    QUERY_UNIT_LIST:'/unit/selectAll',  //查询所有单位
    QUERY_UNIT_ADD:'/unit/add',         //添加单位
    QUERY_UNIT_DELETE:'/unit/delete',   //删除单位
  //报警信息
    QUERY_ALERT_LIST:'/wain/selectAll', //查询报警信息
};



export const models = {
    name: 'adminPage',
    model: {
        state: {
            memberList: [],
            unitList: [],
            alertList: [],
            tagList: [],
            count: 0,
        },
        reducers: {
            setState(state, payLoad) {
                return {
                    ...state,
                    ...payLoad
                }
            }
        },
        effects: dispatch => {
            var i = 1;
            return {
              // 用户
                async getMemberList() {
                    const { code, data } = await axios.post(API.QUERY_MEMBER_LIST);
                    if(  code === 0){
                      dispatch.adminPage.setState({memberList: data})
                    }
                    
                },
                async addMember(params) {
                    try {
                      const { code, data } = await axios.post(API.QUERY_MEMBER_ADD,qs.stringify(params));
                      code === 0 && message.success('添加成功！') && dispatch.adminPage.setState({count:i++});
                    } catch (error) {
                      message.error(error);
                    }
                },
                async editMember(params) {
                    try {
                      const { code, data } = await axios.post(API.QUERY_MEMBER_EDIT,qs.stringify(params));
                      code === 0 && message.success('修改成功！') && dispatch.adminPage.setState({count:i++});
                    } catch (error) {
                      message.error(error);
                    }
                },
                async deleteMember(params) {
                    const { code, data } = await axios.post(API.QUERY_MEMBER_DELETE,qs.stringify(params));
                      code === 0 && dispatch.adminPage.setState({count:i++})  && message.success('删除成功')
                      if (code===3) {
                        message.error('未查询到此用户')
                      }
                    // try {
                      
                    // } catch (error) {
                    //   console.log(error);
                    // }
                    
                },

              //单位
                async getUnitList() {
                    const { code, data } = await axios.post(API.QUERY_UNIT_LIST);
                    code === 0 && dispatch.adminPage.setState({unitList: data})
                },
                async addUnit(params) {
                    const { code, data, msg } = await axios.post(API.QUERY_UNIT_ADD,qs.stringify(params));
                      code === 0 && dispatch.adminPage.setState({count:i++}) && message.success('添加成功！') 
                      code !== 0 && message.error(msg)
                    // try {
                     
                    // } catch (error) {
                    //   console.log(error);
                    //   message.error('权限不足')
                    // }
                    // && dispatch.adminPage.setState({unitList: data})
                },
                async deleteUnit(params) {
                    try {
                      const { code, data } = await axios.post(API.QUERY_UNIT_DELETE,qs.stringify(params));
                      code === 0  && dispatch.adminPage.setState({count:i++});
                      message.success('删除成功')
                    } catch (error) {
                      console.log(error);
                    }
                    // && dispatch.adminPage.setState({unitList: data})
                },

              //报警信息
                async getAlertList() {
                    const params = {
                      time:'newTime',
                      op:'now',
                    }
                    const { code, data } = await axios.post(API.QUERY_ALERT_LIST,qs.stringify(params));
                    code === 34 && dispatch.adminPage.setState({alertList: [{device:'',alertMsg:'无报警信息'}]}) && message.success('无报警信息')
                    code === 0 && dispatch.adminPage.setState({alertList: data})
                },
            }
        }
    }
};
